<?php
header('content-type:application:json;charset=utf8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

chdir(dirname(__FILE__));
error_reporting(E_ERROR);
require_once ("../log.php");
require_once ("../config.php");
require_once ("api_tool.php");
require_once ("api_wxsend_ipad.php");	// ipad 原生函数
require_once ("api_wxsend_cat.php");	// cat 原生函数
require_once ("api_post_cat.php");
require_once ("api_post_ipad.php");

// 微信登录接口
function login_oper($_Data) {
	if (empty($_Data) || empty($_Data['id'])) {
		return array (
			'state' => -1,
			'msg'	=> '输入的参数有误!' 
		);
	}
	$bot_data = simQuery("SELECT * FROM robot AS rot, server AS ser WHERE rot.server_id = ser.id AND rot.id = {$_Data['id']}");
	if (empty($bot_data)) {
		return array (
			'state' => -2,
			'msg'	=> '当前机器人的信息缺失或者有误!'
		);
	}
	// 一是获取可爱猫的 二是获取ipad的登录接口
	if ($bot_data['server_protocol'] == 1) {
		return cat_login_oper($_Data, $bot_data);
	} else {
		return ipad_login_oper($_Data, $bot_data);
	}
}

//	获取微信登录二维码信息
function get_login_state($_Data) {
	if (empty($_Data) || empty($_Data['id'])) {
		return array (
			'state' => -1,
			'msg'	=> '传入参数有误!',
		);
	}

	$bot_data = simQuery("SELECT * FROM robot WHERE id = {$_Data['id']}");
	if (empty($bot_data) || empty($bot_data['wechat_uuid_key'])) {
		return array (
			'state' => -2,
			'msg'	=> 'uuid没有,请重新登录!',
		);
	}

	// 一是获取可爱猫的 二是获取ipad的登录接口
	if ($bot_data['server_protocol'] == 1) {
		return cat_login_state($_Data, $bot_data);
	} else {
		return ipad_login_state($_Data, $bot_data);
	}
}

// 退出微信
function exit_oper($_Data) {
	if (empty($_Data) || empty($_Data['id'])) {
		return array (
			'state' => -1,
			'msg'	=> '传入参数有误!',
		);
	}

	$bot_data = simQuery("SELECT * FROM robot WHERE id = {$_Data['id']}");
	if (empty($bot_data) || empty($bot_data['wechat_id'])) {
		return array (
			'state' => -2,
			'msg'	=> '该机器人没有登录微信!',
		);
	}

	if ($bot_data['server_protocol'] == 1) {
		return cat_exit_oper($_Data, $bot_data);
	} else {
		return ipad_exit_oper($_Data, $bot_data);
	}
}

// 获取心跳接口
function get_heart_beat($_Data) {
	LOGINFO($_Data, '=== $_Data');
	if (empty($_Data) || empty($_Data['id'])) {
		return array (
			'state' => -1,
			'msg'	=> '传入参数有误!',
		);
	}

	$bot_data = simQuery("SELECT * FROM robot WHERE id = {$_Data['id']}");
	if (empty($bot_data) || empty($bot_data['wechat_id'])) {
		return array (
			'state' => -2,
			'msg'	=> '该机器人没有登录微信!',
		);
	}

	if ($bot_data['server_protocol'] == 1) {
		return cat_heart_beat($_Data, $bot_data);
	} else {
		return ipad_heart_beat($_Data, $bot_data);
	}
}

// 发送梯子整理数据(时间段)
function send_ladder_data($_Data) {
	$wx_data = simQuery("SELECT rot.server_protocol AS server_protocol, wx.robot_wxid AS robot_wxid, wx.wechat_room_id AS wechat_room_id FROM wechat_group AS wx, robot AS rot WHERE wx.id = {$_Data['id']} AND wx.robot_id = rot.id;");
	if (empty($wx_data)) {
		return array (
			'state' => -1,
			'msg'	=> '没有当前的微信信息!',
		);
	}

	$shop_list_data = AllQuery("SELECT * FROM item_send_catalog WHERE into_time >= '{$_Data['before_time']}' AND into_time <= '{$_Data['now_time']}' AND wx_id = {$_Data['id']}");
	// LOGINFO($shop_list_data, '=== $shop_list_data');
	if (empty($shop_list_data) || count($shop_list_data) <= 0) {
		return array (
			'state' => -2,
			'msg'	=> '当前微信没有合适的商品!',
		);
	}
	$list_str = '';
	if (!empty($_Data['text'])) {
		$list_str .= "{$_Data['text']}\n";
	}

	for ($i = 0; $i < count($shop_list_data); ++$i) {
		$shop_data = $shop_list_data[$i];
		if ($wx_data['server_protocol'] == '1') {
			$list_str .= "{$shop_data['shop_name']}[@emoji=\\uD83D\\uDCB0]{$shop_data['final_price']}元\n";
		} else {
			$list_str .= "{$shop_data['shop_name']}💰{$shop_data['final_price']}元\n";
		}
	}

	if (!empty($_Data['end_text'])) {
		$list_str .= "\n{$_Data['end_text']}";
	}

	$_wx = array();
	$_wx['bot_wxid'] = $wx_data['robot_wxid'];
	$_wx['from_wxid'] = $wx_data['wechat_room_id'];

	$_item = array();
	$_item['msg'] = $list_str;
	$_item['msg_type'] = 1;
	if ($wx_data['server_protocol'] == '1') {
		cat_send_msg($_wx, $_item);
	} else {
		ipad_send_msg($_wx, $_item);
	}
	
	return array (
		'state' => 1,
		'msg'	=> '成功!',
	);
}

// 选择合适的商品(统一接口)
function select_fit_shop($_Data) {
	// 获取商品分类列表
	$shop_list = AllQuery("SELECT * FROM parent_category");
	$wx_grunp = simQuery("SELECT * FROM wechat_group WHERE id = {$_Data['wx_id']}");
	if (empty($wx_grunp)) {
		return array (
			'state' => -1,
			'msg' 	=> '当前微信群为空!',
		);
	}

	$ids_list = explode(',', $wx_grunp['parent_category_ids']);

	$ids_deter = array();
	foreach ($ids_list as $value) {
		$ids_deter[$value] = true;
	}

	// 判断是否带条件查询
	$frist = true;
	$where = "WHERE channel_id = '{$_Data['channel_id']}'";
	$all_sel = true;

	if (!$ids_deter[0]) {
		foreach ($shop_list as $s_value) {
			if (!empty($ids_deter[$s_value['id']])) {
				$where .= " OR parent_category_ids LIKE '%,{$s_value['id']},%'";
			} else {
				if ($s_value['id'] != '0') {
					$all_sel = false;
				}
			}
		}
	}

	if ($all_sel) {
		$where = "WHERE channel_id = '{$_Data['channel_id']}'";
	}

	// 从优选库选择合适的东西
	if ($wx_grunp['with_preferred']) {
		if ($wx_grunp['send_item_id'] != '') {
			$wx_grunp['send_item_id'] = substr($wx_grunp['send_item_id'], 0, -1);
		}
		$send_item_id = explode(',', $wx_grunp['send_item_id']);
		$send_itemid_list = array();
		foreach ($send_item_id as $value) {
			$send_itemid_list[$value] = true;
		}

		$pre_shop_data = simQuery("SELECT * FROM item_preferred {$where} ORDER BY RAND() LIMIT 1");
		// 没有发送过,发送
		if (!$send_itemid_list[$pre_shop_data['item_id']]) {
			$shop_data = $pre_shop_data;
			$shop_code = 2; // 用于区分优选库和普通库 1是普通库 2是优选库
		}
	}

	// 如果没有选择优选库,优选库没有合适的商品,或者是发送过的商品 就随机普通库的商品
	if (empty($shop_data)) {
		// 随机一条数据
		$shop_data = simQuery("SELECT * FROM item_ordinary {$where} ORDER BY RAND() LIMIT 1");
		$shop_code = 1;
	}

	// 如果数据为空
	if (empty($shop_data)) {
		return array(
			'state'	=> -2,
			'msg' 	=> '当前商品数据为空!',
		);
	}

	$good_img = simQuery("SELECT image FROM parent_category ORDER BY RAND() LIMIT 1")['image'];
	$data = array();
	$data['wx_id'] 		= $_Data['wx_id'];
	$data['bot_id']		= $_Data['bot_id'];
	$data['good_img'] 	= $good_img;
	$data['shop_id']	= $shop_data['item_id'];
	$data['channel_id']	= $_Data['channel_id'];
	$data['shop_code']	= $shop_code;
	$data['fun'] 		= 'send_shop_group';
	$data['sign'] 		= generateSign($data);

	$host = "127.0.0.1";
	$port = 80;
	$url  = "/api/api_accept_post.php";
	return json_decode(sendZSHttp($host, $port, $url, $data, 'post'), true);
}

// 发送一个商品信息到达微信群(此格式为固定格式)
function send_shop_group($_Data) {
	LOGINFO($_Data, '=== $_Data');
	$host = "127.0.0.1";
	$port = 80;
	if ($_Data['channel_id'] == '1') {
		$url = "/api/api_taobao/decide.php";
	} else if ($_Data['channel_id'] == '2') {
		$url = "/api/api_jd/decide.php";
	} else if ($_Data['channel_id'] == '3') {
		$url = "/api/api_pdd/decide.php";
	}
	$re = json_decode(sendZSHttp($host, $port, $url, $_Data), true);
	LOGINFO($re, '=== $re');
	return $re;
}

?>